package org.edith.shiro.config;

import org.edith.shiro.component.ShiroSessionIdGenerator;
import org.edith.shiro.constant.ShiroConstant;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisDAOConfig {
    /**
     * 配置 redis 相关默认值
     */
    @Value("${spring.redis.host:127.0.0.1}")
    private String host;
    @Value("${spring.redis.port:6379}")
    private int port;
    @Value("${spring.redis.timeout:6000}")
    private int timeout;
    @Value("${spring.redis.password:}")
    private String password;
    @Value("${spring.redis.pool.max-active:1000}")
    private int maxActive;
    @Value("${spring.redis.pool.max-idle:10}")
    private int maxIdle;
    @Value("${spring.redis.pool.min-idle:5}")
    private int minIdle;
    @Value("${spring.redis.pool.max-wait:-1}")
    private long maxWaitMillis;

    @Autowired
    @Qualifier("edithSessionIdGenerator")
    private ShiroSessionIdGenerator sessionIdGenerator;

    @Bean
    public JedisPool redisPoolFactory(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setMaxTotal(maxActive);
        jedisPoolConfig.setMinIdle(minIdle);
        return  new JedisPool(jedisPoolConfig,host,port,timeout,password);
    }
    /**
     * 配置Redis管理器,使用的是shiro-redis开源插件
     */
    @Bean("edithRedisManager")
    public RedisManager redisManager() {
        RedisManager redisManager = new RedisManager();
        redisManager.setHost(host);
        redisManager.setPort(port);
        redisManager.setTimeout(timeout);
        redisManager.setPassword(password);
        redisManager.setJedisPool(redisPoolFactory());
        return redisManager;
    }

    /**
     * 配置RedisSessionDAO
     * 使用的是shiro-redis开源插件
     */
    @Bean("edithRedisSessionDAO")
    public RedisSessionDAO redisSessionDAO() {
        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
        redisSessionDAO.setRedisManager(redisManager());
        redisSessionDAO.setSessionIdGenerator(sessionIdGenerator);
        redisSessionDAO.setKeyPrefix(ShiroConstant.SESSION_KEY);
        redisSessionDAO.setExpire(ShiroConstant.EXPIRE);
        return redisSessionDAO;
    }

    /**
     * 配置Cache管理器
     * 用于往Redis存储权限和角色标识,使用的是shiro-redis开源插件
     */
    @Bean("edithRedisCacheManager")
    public RedisCacheManager cacheManager() {
        RedisCacheManager redisCacheManager = new RedisCacheManager();
        redisCacheManager.setRedisManager(redisManager());
        redisCacheManager.setKeyPrefix(ShiroConstant.CACHE_KEY);
        // 配置缓存的话要求放在session里面的实体类必须有个id标识
        redisCacheManager.setPrincipalIdFieldName("username");
        return redisCacheManager;
    }
}
